// pages/movie/movie.js
var uploadTask;
const recorderManager = wx.getRecorderManager()

Page({
  
  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  }

  /**
   * 页面的初始数据
   */
  /*
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },
  */,
  swiperHideOrShow:function(e){
    this.setData({
      isShow: !e.detail.value
    });
  },
  data: {
    markers: [{
      iconPath: "/resources/others.png",
      id: 0,
      latitude: 23.099994,
      longitude: 113.324520,
      width: 50,
      height: 50
    }],
    polyline: [{
      points: [{
        longitude: 113.3245211,
        latitude: 23.10229
      }, {
        longitude: 113.324520,
        latitude: 23.21229
      }],
      color: "#FF0000DD",
      width: 2,
      dottedLine: true
    }],
    controls: [{
      id: 1,
      iconPath: '/resources/location.png',
      position: {
        left: 0,
        top: 300 - 50,
        width: 50,
        height: 50
      },
      clickable: true
    }],
    isShow:false,
    name:"helloworld",
    movies:[],
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  loadDataFromServer:function(){
    var p = this;
    // 
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: function (res) {
        wx.request({
          url: 'http://ui5.hfbdqn.cn/test/uploadfile.php', //仅为示例，并非真实的接口地址
          data: {
            x: '',
            y: ''
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res.data)
            p.setData({
              filelist: res.data
            });
            wx.hideLoading()
          }
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  cancelUpload:function(){
    uploadTask.abort();
  },
  stopRecord:function(){
    recorderManager.stop()
  },
 startRecord:function(){
   var p = this;
   recorderManager.start({format:"mp3"})

   recorderManager.onStop((res) => {
     console.log('recorder stop', res)
     const { tempFilePath } = res

     wx.uploadFile({
       url: 'http://ui5.hfbdqn.cn/test/upload.php', 
       filePath: tempFilePath,
       name: 'file',
       formData: {
         'user': 'test'
       },
       success: function (res) {
         var data = res.data
         p.loadDataFromServer();
         //do something
       }
     })

    //获得微信的唯一的音频上下文
    /*
     const innerAudioContext = wx.createInnerAudioContext()
     innerAudioContext.autoplay = true
     innerAudioContext.src = tempFilePath
     innerAudioContext.onPlay(() => {
       console.log('开始播放')
     })
     innerAudioContext.onError((res) => {
       console.log(res.errMsg)
       console.log(res.errCode)
     })
     */
     
   })
 },
  onLoad: function (options) {

    this.loadDataFromServer();

    /*
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths

        //上传任务
          uploadTask = wx.uploadFile({
          url: 'http://ui5.hfbdqn.cn/test/upload.php', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: function (res) {
            var data = res.data
            //do something
          }
        })

        uploadTask.onProgressUpdate((res) => {
          console.log('上传进度', res.progress)
          p.setData({
            pic_progress:res.progress
          });
          console.log('已经上传的数据长度', res.totalBytesSent)
          console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
        })

        
      }
    })
    */
    /*
    var p = this;

    wx.showLoading({
      title: '加载中',
      mask: true,
      success: function(res) {
        wx.request({
          url: 'http://api.cntv.cn/epg/epginfo?serviceId=shiyi&d=20180330&c=cctv1', //仅为示例，并非真实的接口地址
          data: {
            x: '',
            y: ''
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res.data)
            p.setData({
              movies: res.data.cctv1.program
            });
            wx.hideLoading()
          }
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  */
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})